var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    is_reply: false,
    is_share:false,
    animationData: {},
    is_gift: false,
    video_fit: 'cover',
    video_list:[],
    page:0,
    info:[],
    reply_text: '填写评论……',
    reply_nickname: '',
    reply_openid: '',
    icon_list: [
      { icon: 'zan', text: '赞', func:'zan' },
      { icon: 'reply', text: '评论', func: 'reply' },
      // { icon: 'msg', text: '私聊' },
      { icon: 'share', text: '分享',func:"share" },
      { icon: 'gif', text: '礼物', func: 'gift' },
    ]
  },
  zhibo:function(){
    wx.navigateTo({
      url: '/pages/quan/video/list?idx=1',
    })
  },
  guanzhu:function(){
    wx.navigateTo({
      url: '/pages/quan/video/list',
    })
  },
  change:function(){
    var that = this;
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/itemRandom/openid/' + app.globalData.openid + '/id/' + that.data.info.id , function (res) {
      if(res.data.err == 2){
        wx.showToast({
          title: '没有更多了',
          icon: "none",
          duration: 2000
        })
      }else{
        that.setData({
          info: res.data
        })
        wx.hideLoading();
      }
      
    });
  },
  follow: function (e) {
    var info = this.data.info;
    var that = this;
    wx.showLoading({
      title: '加载中…',
    })
    var data = {};
    data.openid = app.globalData.openid;
    data.follow_openid = info.follow_openid;
    app.ihttpPost('https://www.58zltc.com/api.php/Quan/follow', data, function (res) {
      if (res.data.err_code === 0) {
        if (res.data.err_msg == '-1') {
          info.is_follow = false;
        } else {
          info.is_follow = true;
        }
        that.setData({
          info: info
        })
        wx.hideLoading()
      } else {
        wx.showToast({
          title: res.data.err_msg
        })
      }
    })
  },
  move:function(e){
    console.log(e.touches[0].pageY);
  },
  reBg: function () {
    this.setData({
      is_reply: false,
      video_fit: 'cover'
    })
  },
  share:function(){
    this.setData({
      is_share: true
    })
  },
  closeShare: function () {
    this.setData({
      is_share: false
    })
  },
  backto: function (e) {
    wx.navigateBack({})
  },
  replyInputBind:function(e){
    this.setData({
      replyInput: e.detail.value
    })
  },
  reply: function () {
    this.setData({
      is_reply: true, 
      video_fit: 'contain'
    })
  },
  comment: function (e) {
    var reply_nickname = e.currentTarget.dataset.nickname;
    var reply_openid = e.currentTarget.dataset.openid;
    var reply_text = "回复" + reply_nickname + ":";
    var openid = app.globalData.openid;
    if (reply_openid != openid) {
      console.log(reply_openid);
      this.setData({
        focus: true,
        reply_text: reply_text,
        reply_openid: reply_openid,
        reply_nickname: reply_nickname,
        bottom: 50,
      })
    }

  },
  setReplyText: function (e) {
    this.setData({
      reply_text: '填写评论……',
      reply_openid: '',
      bottom: 0,
    })
  },
  replyClose:function(){
    this.setData({
      is_reply: false,
    })
  },
  goReply: function(e){
    let that = this;
    var value = that.data.replyInput;
    if(!value){
      wx.showModal({
        title: '提示',
        content: '请输入评论内容！',
        showCancel: false,
        confirmText: '我知道了'
      })
      return false;
    }
    var post_data = [];
    post_data.id = that.data.info.id;
    post_data.content = value;
    post_data.openid = app.globalData.openid;
    post_data.reply_openid = that.data.reply_openid;
    post_data.reply_nickname = that.data.reply_nickname;
    post_data.reply_part = 'video';
    app.ihttpPost("https://www.58zltc.com/api.php/Quan/reply", post_data, function (res) {
      if (res.data.err_code === 400) {
        wx.showToast({
          title: res.data.err_msg,
        })
      } else if (res.data.err_code === 0) {
        var reply_num = that.data.info.reply_num + 1;
        var info_reply_num = "info.reply_num";
        var info_reply_list = "info.reply_list";
        that.setData({
          [info_reply_num]:reply_num,
          [info_reply_list]: that.data.info.reply_list.concat(res.data.data)
        })
        console.log(that.data);
        // wx.showToast({
        //   title: res.data.err_msg,
        //   icon: 'success'
        // })
        //that.reBg();
      }
    })
  },
  gift: function (e) {
    var id = this.data.info.id;
    this.setData({
      is_gift: true,
      gift_id: id
    })
  },
  hiddenGift: function (e) {
    this.setData({
      is_gift: false
    })
  },
  setGift: function (e) {
    var idx = e.currentTarget.dataset.idx;
    let that = this;
    var gift_list = that.data.gift_list;
    gift_list.forEach(function (value, key) {
      gift_list[key].active = key == idx ? true : false;
    })
    this.setData({
      gift_list: gift_list,
      active_gift: gift_list[idx]
    })
  },
  payGift: function (e) {
    let that = this;
    var data = this.data;
    var post_data = {};
    post_data.type = 'quan';
    post_data.to = data.info.follow_openid;
    post_data.from = app.globalData.openid;
    post_data.id = data.id;
    post_data.gift_id = data.active_gift.id;

    app.ihttpPost('https://www.58zltc.com/api.php/Quan/payGift', post_data, function (res) {
      if (res.data.err_code == 400) {
        wx.showToast({
          title: res.data.err_msg,
        })
      } else {
        var data = res.data;
        wx.requestPayment({
          'appId': data.appId,
          'timeStamp': data.timeStamp,
          'nonceStr': data.nonceStr,
          'package': data.package,
          'signType': 'MD5',
          'paySign': data.paySign,
          'success': res => {
            if (res.errMsg == 'requestPayment:ok') {
              wx.showToast({
                title: '赠送成功！',
              })
            }
          }
        })
        that.reBg();
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '加载中…',
    })
    this.setData({
      id: options.id
    })
    console.log(1111);
    const id = options.id;
    console.log(id);
    let that = this;
    if (id == undefined){
      app.ihttpGet('https://www.58zltc.com/api.php/Quan/itemRandom/openid/' + app.globalData.openid + '/id/' + id, function (res) {
        that.setData({
          info: res.data
        })
        wx.hideLoading()
      });
    }else{
      app.ihttpGet('https://www.58zltc.com/api.php/Quan/item/openid/' + app.globalData.openid + '/id/'+id, function (res) {
        that.setData({
          info: res.data
        })
        wx.hideLoading()
      });
    }
    
    // app.ihttpGet('https://www.58zltc.com/api.php/Quan/item/openid/' + app.globalData.openid + '/id/'+id, function (res) {
    //   that.setData({
    //     info: res.data
    //   })
    //   wx.hideLoading()
    // });
    // app.ihttpGet('https://www.58zltc.com/api.php/Quan/getVideoList/openid/' + app.globalData.openid + '/id/' + id +'/page/' + 0 + "/openid/" + app.globalData.openid, function (res) {
    //   that.setData({
    //     video_list: res.data,
    //     info:res.data[0]
    //   })
    //   console.log(1111);
    //   console.log(that.data.info);
    //   wx.hideLoading()
    // });
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/giftList', function (res) {
      that.setData({
        gift_list: res.data
      })
    });
  },
  bindChange:function(e){
    var that = this;
    var current = e.detail.current;
    var id = that.data.video_list[current]['id'];
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/item/openid/' + app.globalData.openid + '/id/' + id, function (res) {
      that.setData({
        info: res.data
      })
      console.log(that.data.info);
    });
    if((current +1 )%3 == 0){
      var page = (current+1)/3;
      app.ihttpGet('https://www.58zltc.com/api.php/Quan/getVideoList/openid/' + app.globalData.openid + '/id/' + that.data.id + '/page/' + page, function (res) {
        that.setData({
          video_list: that.data.video_list.concat(res.data)
        })
        wx.hideLoading()
      });
      
    }
  },
  zan: function (e) {
    var info = this.data.info;
    var that = this;
    
    app.ihttpGet('https://www.58zltc.com/api.php/Quan/zan/openid/' + app.globalData.openid + '/id/' + info.id, function(res){
      if (res.data.err_code === 0) {
        if (res.data.err_msg == '-1') {
          info.zan_num = parseInt(info.zan_num) - 1;
          info.is_zan = false;
        } else {
          info.zan_num = parseInt(info.zan_num) + 1;
          info.is_zan = true;
        }
        that.setData({
          info: info
        })
      
      } else {
        wx.showToast({
          title: res.data.err_msg
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
    })

    this.animation = animation

    // animation.scale(2, 2).rotate(45).step()

    this.setData({
      animationData: animation.export()
    })
    var n = 0;
    //连续动画需要添加定时器,所传参数每次+1就行
    setInterval(function () {
      // animation.translateY(-60).step()
      n = n + 1;
      this.animation.rotate(10 * (n)).step()
      this.setData({
        animationData: this.animation.export()
      })
    }.bind(this), 200)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})